home *** CD-ROM | disk | FTP | other *** search
/ The Arcade BBS / arcadebbs.zip / arcadebbs / bbstools / WWIV Mods / WSFTPSRC.ZIP / WS_HOST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-12  |  26.0 KB  |  763 lines

  1. /***************************************************************************
  2.   Windows Sockets Client Application Support Module
  3.  
  4.   Written by:
  5.       John A. Junod             Internet: <junodj@gordon-emh2.army.mil>
  6.       267 Hillwood Street                 <zj8549@trotter.usma.edu>
  7.       Martinez, GA 30907      Compuserve: 72321,366 
  8.  
  9.   This program executable and all source code is released into the public
  10.   domain.  It would be nice (but is not required) to give me a little 
  11.   credit for any use of this code.  
  12.  
  13.   THE INFORMATION AND CODE PROVIDED IS PROVIDED AS IS WITHOUT WARRANTY 
  14.   OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  15.   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  16.   PURPOSE. IN NO EVENT SHALL JOHN A. JUNOD BE LIABLE FOR ANY DAMAGES 
  17.   WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS 
  18.   OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF JOHN A. JUNOD HAS BEEN 
  19.   ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  20.  
  21. *****************************************************************************/
  22.  
  23. #include "ws_glob.h"
  24. #include "ws_ftp.h"
  25.  
  26. #define MAXHOSTS 20
  27. char szInitDir[80];
  28.  
  29.  
  30. struct HOSTINFO {
  31.   HANDLE hostname;
  32.   HANDLE userid;
  33.   HANDLE password;
  34.   HANDLE initdir;
  35.   int hosttype;
  36.   int sendsize;
  37.   int recvsize;
  38.   u_int timeout;
  39. };
  40.  
  41. HANDLE HostInfo[MAXHOSTS];
  42.  
  43. // int nTypes[MAXHOSTS];
  44. BOOL bSaveUID=FALSE;
  45. BOOL bSavePWD=FALSE;
  46. char szCrypt[160];
  47. LPSTR szAnony="anonymous";
  48. int nHostType=0;
  49. extern BOOL bHELP;
  50. extern BOOL bCanMKD,bCanRMD,bCanREN,bCanDELE;
  51. extern HBRUSH hbrGray1,hbrGray2;
  52.  
  53. /*
  54. // this encryption is not secure nor is it intended to be
  55. // this is just to keep the password from being plain text
  56. // in the ini file.  I'd really recommend people don't save
  57. // their passwords
  58. */
  59. LPSTR EnCrypt(LPSTR userid,LPSTR passwd)
  60. {
  61.   int nIndex;
  62.   if(lstrcmp(userid,szAnony)==0)
  63.     return(passwd);
  64.   szCrypt[0]=0;
  65.   for(nIndex=0;nIndex<lstrlen(passwd);nIndex++) {
  66.     wsprintf(&szCrypt[nIndex*2],"%02X",
  67.       ((char)passwd[nIndex])+nIndex);
  68.   }
  69.   return(szCrypt);
  70. }
  71.  
  72. int unhex(char c) {
  73.   if(c>'9') return(c-'7');
  74.   return (c-'0');
  75. }
  76.  
  77. LPSTR DeCrypt(LPSTR userid,LPSTR passwd)
  78. {
  79.   int nIndex;
  80.   if(lstrcmp(userid,szAnony)==0)
  81.     return(passwd);
  82.   szCrypt[0]=0;
  83.   for(nIndex=0;nIndex<lstrlen(passwd);nIndex+=2) {
  84.     (BYTE)szCrypt[nIndex/2]=
  85.       ((unhex(passwd[nIndex])*16)+
  86.        unhex(passwd[nIndex+1]))-(nIndex/2);
  87.     szCrypt[nIndex/2+1]=0;
  88.   }
  89.   return(szCrypt);
  90. }
  91.  
  92. /*************************************************************
  93.   allocate global memory and copy the string into the memory
  94. */
  95. HANDLE SaveOne(LPSTR lpszSrc)
  96. {
  97.   HANDLE pGlobal;
  98.  
  99.   if(lpszSrc==NULL || strlen(lpszSrc)==0) return(NULL);
  100.  
  101.   if((pGlobal=GlobalAlloc(LHND,lstrlen(lpszSrc)+1))!=NULL){
  102.     strcpy(GlobalLock(pGlobal),lpszSrc);
  103.     GlobalUnlock(pGlobal);
  104.   }
  105.   return(pGlobal);
  106. }
  107.  
  108. /*************************************************************
  109.   get a string from global memory
  110. */
  111. void GetOne(LPSTR lpszDest,HANDLE pGlobal)
  112. {
  113.   if(pGlobal) {
  114.     strcpy(lpszDest,GlobalLock(pGlobal));
  115.     GlobalUnlock(pGlobal);
  116.   } else *lpszDest=0;
  117. }
  118.  
  119. /*************************************************************
  120.   create our host information structure in global memory
  121. */
  122. HANDLE MakeInfo(LPSTR hostname,LPSTR userid,LPSTR password,LPSTR initdir,
  123.   int hosttype,int sendsize,int recvsize,u_int timeout)
  124. {
  125.   HANDLE pGlobal;
  126.   struct HOSTINFO *pHost;
  127.   if((pGlobal=GlobalAlloc(LHND,sizeof(struct HOSTINFO)))!=NULL) {
  128.     pHost=(struct HOSTINFO *)GlobalLock(pGlobal);
  129.     pHost->hostname=SaveOne(hostname);
  130.     pHost->userid=SaveOne(userid);
  131.     pHost->password=SaveOne(password);
  132.     pHost->initdir=SaveOne(initdir);
  133.     pHost->hosttype=hosttype;
  134.     pHost->sendsize=sendsize;
  135.     pHost->recvsize=recvsize;
  136.     pHost->timeout=timeout;
  137.     GlobalUnlock(pGlobal);    
  138.   }
  139.   return(pGlobal);
  140. }
  141.  
  142. /*************************************************************
  143.   release a global memory host information structure
  144. */
  145. void FreeInfo(HANDLE pGlobal)
  146. {
  147.   struct HOSTINFO *pHost;
  148.   if(pGlobal && (pHost=(struct HOSTINFO *)GlobalLock(pGlobal))!=NULL) {
  149.     if(pHost->hostname) GlobalFree(pHost->hostname);
  150.     if(pHost->userid)   GlobalFree(pHost->userid);
  151.     if(pHost->password) GlobalFree(pHost->password);
  152.     if(pHost->initdir)  GlobalFree(pHost->initdir);
  153.     GlobalUnlock(pGlobal);
  154.     GlobalFree(pGlobal);
  155.   }
  156. }
  157.  
  158. /****************************************************************
  159.   get the information from a global memory host info structure
  160. */
  161. BOOL GetInfo(HANDLE pGlobal,LPSTR hostname,LPSTR userid,
  162.   LPSTR password,LPSTR initdir,
  163.   int *hosttype,int *sendsize,int *recvsize,u_int *timeout)
  164. {
  165.   struct HOSTINFO *pHost;
  166.   if(pGlobal && (pHost=(struct HOSTINFO *)GlobalLock(pGlobal))!=NULL) {
  167.     if(hostname) GetOne(hostname,pHost->hostname);
  168.     if(userid)   GetOne(userid  ,pHost->userid);
  169.     if(password) GetOne(password,pHost->password);
  170.     if(initdir)  GetOne(initdir ,pHost->initdir);
  171.     if(hosttype) *hosttype=pHost->hosttype;
  172.     if(sendsize) *sendsize=pHost->sendsize;
  173.     if(recvsize) *recvsize=pHost->recvsize;
  174.     if(timeout)  *timeout =pHost->timeout;
  175.     GlobalUnlock(pGlobal);
  176.     return(TRUE);
  177.   }
  178.   return(FALSE);
  179. }
  180.  
  181. /***************************************************************
  182.   compare a hostname to a global memory host info structure
  183. */
  184. BOOL CompareHost(LPSTR lpszHost,HANDLE pGlobal)
  185. {
  186.   BOOL retcode=FALSE;
  187.   char Hostname[120];
  188.   if(GetInfo(pGlobal,Hostname,NULL,NULL,NULL,NULL,NULL,NULL,NULL))
  189.       if(lstrcmpi(Hostname,lpszHost)==0) retcode=TRUE;
  190.   return(retcode);
  191. }
  192.  
  193. /*******************************************************
  194.   Set the dialog box controls according to the current
  195.   selected host.
  196. */
  197. SetDefaultHostStuff(HWND hWndDlg,LPSTR szRHost) {
  198.   int nIndex;
  199.   struct HOSTINFO *pHost;
  200.   LPSTR pUserid,pPasswd,pStr;
  201.      
  202.   for(nIndex=0;nIndex<MAXHOSTS;nIndex++)
  203.     if(CompareHost(szRHost,HostInfo[nIndex])&&
  204.     (pHost=(struct HOSTINFO *)GlobalLock(HostInfo[nIndex]))!=NULL) {
  205.       if(pHost->userid && (pUserid=GlobalLock(pHost->userid))!=NULL) {
  206.         SetDlgItemText(hWndDlg,DLG_EDT_USERID,pUserid);
  207.         if(lstrcmpi(pUserid,szAnony)==0)
  208.           CheckDlgButton(hWndDlg,DLG_HOST_ANONY,TRUE);
  209.         else
  210.           CheckDlgButton(hWndDlg,DLG_HOST_ANONY,FALSE);
  211.         if(pHost->userid && (pPasswd=GlobalLock(pHost->password))!=NULL) {
  212.           SetDlgItemText(hWndDlg,DLG_EDT_PASSWD,DeCrypt(pUserid,pPasswd));
  213.           GlobalUnlock(pHost->password);
  214.           CheckDlgButton(hWndDlg,DLG_HOST_PWD,TRUE);
  215.         } else {
  216.           if(lstrcmpi(pUserid,szAnony)==0) {
  217.             if(*szMailAddress==0)
  218.               StdInput(szMailAddress,"Enter your e-mail address:");
  219.             SetDlgItemText(hWndDlg,DLG_EDT_PASSWD,szMailAddress);
  220.           } else SetDlgItemText(hWndDlg,DLG_EDT_PASSWD,"");
  221.           CheckDlgButton(hWndDlg,DLG_HOST_PWD,FALSE);
  222.         }
  223.         GlobalUnlock(pHost->userid);
  224.       } else {
  225.         SetDlgItemText(hWndDlg,DLG_EDT_USERID,"");
  226.         SetDlgItemText(hWndDlg,DLG_EDT_PASSWD,"");
  227.       }
  228.       CheckRadioButton(hWndDlg,DLG_HOST_AUTO,DLG_HOST_U5000,6000+pHost->hosttype);
  229.       if(pHost->initdir && (pStr=GlobalLock(pHost->initdir))!=NULL) {
  230.         SetDlgItemText(hWndDlg,DLG_HOST_DIR,pStr);
  231.         GlobalUnlock(pHost->initdir);
  232.       } else {
  233.         SetDlgItemText(hWndDlg,DLG_HOST_DIR,"");
  234.       }
  235.       SetDlgItemInt(hWndDlg,DLG_HOST_TIMEOUT,pHost->timeout,FALSE);
  236.       GlobalUnlock(HostInfo[nIndex]);
  237.       break;
  238.     }
  239.   return TRUE;
  240. }
  241.  
  242. /******************************************************************
  243.   Message processing for host dialog box
  244. */
  245. BOOL FAR PASCAL WS_HostMsgProc(HWND hWndDlg, WORD Message,
  246.                                WORD wParam, LONG lParam)
  247. {
  248.   int nIndex;
  249.   char szRHost[80];
  250.   UINT nRC;
  251.  
  252.   switch(Message)
  253.   {
  254.     case WM_INITDIALOG:
  255.       for(nIndex=0;nIndex<MAXHOSTS;nIndex++)
  256.         if(HostInfo[nIndex] && GetInfo(HostInfo[nIndex],szRHost,NULL,
  257.            NULL,NULL,NULL,NULL,NULL,NULL))
  258.           SendDlgItemMessage(hWndDlg,DLG_EDT_HOST,CB_ADDSTRING,
  259.             0,(long)szRHost);
  260.       SetDlgItemText(hWndDlg,DLG_EDT_HOST,szRemoteHost);
  261.       SetDlgItemText(hWndDlg,DLG_EDT_USERID,szUserID);
  262.       SetDlgItemText(hWndDlg,DLG_EDT_PASSWD,szPassWord);
  263.       SetDlgItemText(hWndDlg,DLG_HOST_DIR,szInitDir);
  264.       SetDlgItemInt(hWndDlg,DLG_HOST_TIMEOUT,uiTimeOut,FALSE);
  265.       CheckDlgButton(hWndDlg,DLG_HOST_PWD,bSavePWD);
  266.       CheckRadioButton(hWndDlg,DLG_HOST_AUTO,DLG_HOST_U5000,6000+nHostType);
  267.       strcpy(szRHost,szRemoteHost);
  268.       SetDefaultHostStuff(hWndDlg,szRHost);
  269.       cwCenter(hWndDlg, 0);
  270.       SendDlgItemMessage(hWndDlg,DLG_EDT_HOST,WM_SETREDRAW,TRUE,0l);
  271.       break;
  272.     case WM_CLOSE:
  273.       PostMessage(hWndDlg, WM_COMMAND, IDCANCEL, 0L);
  274.       break;
  275.     case WM_CTLCOLOR:
  276.       switch(HIWORD(lParam)) {
  277.         case CTLCOLOR_BTN:
  278.           if(LOWORD(lParam)<10) return((LRESULT)NULL);
  279.         case CTLCOLOR_DLG:
  280.         case CTLCOLOR_STATIC:
  281.           SetBkColor((HDC) wParam, RGB(192,192,192));
  282.           return(LRESULT)hbrGray1;
  283.         }
  284.         return(LRESULT)NULL;
  285.  
  286.     case WM_COMMAND:
  287.       switch(wParam)
  288.       {
  289.         case DLG_EDT_HOST:
  290.           if(HIWORD(lParam)==CBN_KILLFOCUS ||
  291.              HIWORD(lParam)==CBN_EDITCHANGE) {
  292.             GetDlgItemText(hWndDlg,DLG_EDT_HOST,szRHost,70);
  293.           } else if(HIWORD(lParam)==CBN_SELCHANGE) {
  294.             if((nIndex=SendDlgItemMessage(hWndDlg,DLG_EDT_HOST,
  295.               CB_GETCURSEL,0,0L))!=LB_ERR)
  296.             SendDlgItemMessage(hWndDlg,DLG_EDT_HOST,CB_GETLBTEXT,
  297.               nIndex,(LONG)szRHost);
  298.             else break;
  299.           } else break;
  300.           SetDefaultHostStuff(hWndDlg,szRHost);
  301.           break;
  302.  
  303.         case DLG_HOST_ANONY:
  304.           if(IsDlgButtonChecked(hWndDlg,DLG_HOST_ANONY)) {
  305.             SetDlgItemText(hWndDlg,DLG_EDT_USERID,szAnony);
  306.             if(*szMailAddress==0)
  307.               StdInput(szMailAddress,"Enter your e-mail address:");
  308.             SetDlgItemText(hWndDlg,DLG_EDT_PASSWD,szMailAddress);
  309.           }
  310.           return(FALSE);
  311.         case DLG_HOST_PWD:
  312.         case DLG_HOST_SAVE:
  313.           if(!IsDlgButtonChecked(hWndDlg,DLG_HOST_SAVE))
  314.             CheckDlgButton(hWndDlg,DLG_HOST_PWD,FALSE);
  315.           return(FALSE);
  316.         case IDOK:
  317.           GetDlgItemText(hWndDlg,DLG_EDT_HOST,szRemoteHost,70);
  318.           GetDlgItemText(hWndDlg,DLG_EDT_USERID,szUserID,15);
  319.           GetDlgItemText(hWndDlg,DLG_EDT_PASSWD,szPassWord,50);
  320.           GetDlgItemText(hWndDlg,DLG_HOST_DIR,szInitDir,79);
  321.           nRC=GetDlgItemInt(hWndDlg,DLG_HOST_TIMEOUT,NULL,FALSE);
  322.           if(nRC==0) uiTimeOut=65; else uiTimeOut=nRC;
  323.           if(uiTimeOut > (65536/1000)) uiTimeOut=65530/1000;
  324.           bSavePWD=FALSE;
  325.           if(IsDlgButtonChecked(hWndDlg,DLG_HOST_PWD))
  326.             bSavePWD=TRUE;
  327.           else
  328.             bSavePWD=FALSE;
  329.           nHostType=0;
  330.           for(nIndex=1;nIndex<=DLG_HOST_U5000;nIndex*=2)
  331.             if(IsDlgButtonChecked(hWndDlg,6000+nIndex))
  332.               nHostType=nIndex;
  333.           EndDialog(hWndDlg, TRUE);
  334.           break;
  335.         case IDCANCEL:
  336.           EndDialog(hWndDlg, FALSE);
  337.           break;
  338.       }
  339.       break;
  340.     default:
  341.       return FALSE;
  342.   }
  343.   return TRUE;
  344. }
  345.  
  346. /***************************************************************
  347.   save the information about the current host in the array of
  348.   host information.  Called from connect routine.
  349. */
  350. void SaveHostName(LPSTR szRemoteHost,LPSTR szUserid,LPSTR szPassword)
  351. {
  352.   int nIndex;
  353.   BOOL bFound=FALSE;
  354.   struct HOSTINFO *pHost;
  355.                 
  356.   for(nIndex=0;nIndex<MAXHOSTS;nIndex++)
  357.     if(CompareHost(szRemoteHost,HostInfo[nIndex])&&
  358.     ((LPSTR)pHost=GlobalLock(HostInfo[nIndex]))!=NULL) {
  359.       bFound=TRUE;
  360.       if(pHost->userid)   GlobalFree(pHost->userid);
  361.       if(pHost->password) GlobalFree(pHost->password);
  362.       if(pHost->initdir)  GlobalFree(pHost->initdir);
  363.       pHost->userid=SaveOne(szUserid);
  364.       if(bSavePWD)
  365.         pHost->password=SaveOne(EnCrypt(szUserid,szPassword));
  366.       else
  367.         pHost->password=NULL;
  368.       pHost->initdir=SaveOne(szInitDir);
  369.       pHost->hosttype=nHostType;
  370.       pHost->timeout=uiTimeOut;
  371.       GlobalUnlock(HostInfo[nIndex]);
  372.     }
  373.  
  374.   if(!bFound) {
  375.     FreeInfo(HostInfo[MAXHOSTS-1]);
  376.     for(nIndex=(MAXHOSTS-1);nIndex>0;nIndex--)
  377.       HostInfo[nIndex]=HostInfo[nIndex-1];
  378.     HostInfo[0]=MakeInfo(szRemoteHost,szUserid,
  379.       (bSavePWD?EnCrypt(szUserid,szPassword):NULL),
  380.       szInitDir,nHostType,1024,1024,uiTimeOut);
  381.   }
  382. }
  383.  
  384. /*****************************************************
  385.   convert remotename into something DOS can deal with
  386. */
  387. MakeLocalName(LPSTR localname,LPSTR remotename)
  388. {
  389.   int nIndex;
  390.   char Name[10],Ext[4],*s;
  391.  
  392.   while(*remotename!=0 && *remotename=='.')
  393.     remotename++;
  394.   for(nIndex=0;nIndex<8;nIndex++)
  395.     if(*remotename!=0 && *remotename!='.' && *remotename!=' ')
  396.       Name[nIndex] = *remotename++;
  397.     else break;
  398.   Name[nIndex]=0; Ext[0]=0;
  399.   if((s=strchr(remotename,'.'))!=NULL)
  400.     remotename=s;
  401.   while(*remotename!=0 && (*remotename=='.' || *remotename==' '))
  402.     remotename++;
  403.   if(*remotename!=0) {
  404.     for(nIndex=0;nIndex<3;nIndex++)
  405.       if(*remotename!=0 && *remotename!='.' && *remotename!=' ')
  406.         Ext[nIndex] = *remotename++;
  407.       else break;
  408.     Ext[nIndex]=0;
  409.   }
  410.   if(Ext[0]==0) {
  411.     lstrcpy(localname,Name);
  412.   } else {
  413.     wsprintf(localname,"%s.%s",Name,Ext);
  414.   }
  415.  
  416.   if(lstrlen(localname)==0) {
  417.     lstrcpy(Name,"aaremote");
  418.     lstrcpy(localname,Name);
  419.   }
  420.  
  421.   if(bRecvUniq) {
  422.     nIndex=0;
  423.     while((int)access(localname,0)==0 && nIndex<99) {
  424.       DoPrintf("[recvuniq] %s - %s - %s",Name,Ext,localname);
  425.       if(Ext[0]==0)
  426.         wsprintf(localname,"%s.%03u",Name,nIndex);
  427.       else if(lstrlen(Name)>5)
  428.         wsprintf(localname,"%-5.5s%03u.%s",Name,nIndex,Ext);
  429.       else wsprintf(localname,"%s%03u.%s",Name,nIndex,Ext);
  430.       nIndex++;
  431.     }
  432.   }
  433.   return(TRUE);
  434. }
  435.  
  436. /*************************************************
  437.   find filename in a UNIX directory listing
  438. */
  439. LPSTR FindName(LPSTR szLine)
  440. {
  441.   int nIndex;
  442.   char *pStr;
  443.  
  444.   // strip trailing garbage from the line if there is any.
  445.   while((nIndex=strlen(szLine))>2 &&
  446.     (szLine[nIndex-1]==0x0a || szLine[nIndex-1]==0x0d || 
  447.      szLine[nIndex-1]==' ' || szLine[nIndex-1]==0x09))
  448.     szLine[nIndex]=0;
  449.  
  450.   // now the name SHOULD be the last thing on the line
  451.   if((pStr=strrchr(szLine,' '))!=NULL ||
  452.      (pStr=strrchr(szLine,0x09))!=NULL) {
  453.     while(*pStr && (*pStr==' ' || *pStr==0x09)) pStr++;
  454.     return(pStr);
  455.   }
  456.   return(szLine);
  457. }
  458.  
  459. /*****************************************************************
  460.   This is the routine that take the output from LIST and breaks
  461.   it down into files and directories.  The format for the output
  462.   from most of the machine types was provided by Chris Sacksteder.
  463. */
  464. int GetRemoteDirForWnd(HWND hWnd)
  465. {
  466.   char *pStr,*s;
  467.   FILE *fd;
  468.   int nRC;
  469.  
  470.   // clean out the old contents of the list boxes
  471.   SendMessage(hLbxRDir,LB_RESETCONTENT,0,0);
  472.   SendMessage(hLbxRFiles,LB_RESETCONTENT,0,0);
  473.   // can't do much if we aren't connected
  474.   if(!bConnected) {
  475.     SendMessage(hTxtRDir,WM_SETTEXT,0,(LPARAM)"");
  476. //    SendMessage(hTxtStatus,WM_SETTEXT,0,
  477. //              (LPARAM)"not connected to remote host");
  478.   }
  479.   else {
  480.     // get the remote directory name
  481.     strcpy(szString,"undecipherable");
  482.     nRC=DoPWD(ctrl_socket);
  483.     if(nRC==FTP_COMPLETE) {
  484.       if((pStr=strchr(szMsgBuf,'"'))!=NULL)
  485.         strncpy(szString,++pStr,180);
  486.       if((pStr=strchr(szString,'"'))!=NULL)
  487.         *pStr=0;
  488.       else szString[180]=0;
  489.     }
  490.     SendMessage(hTxtRDir,WM_SETTEXT,0,(LPARAM)szString);
  491.     // if we haven't done it already then go try to determine
  492.     // host type and what are valid functions. 
  493.     if(!bHELP) ReadProcessHelp(ctrl_socket);
  494.     // go get the current remote directory listing in tmpfile.tmp
  495.     nRC=DoDirList(ctrl_socket,"LIST");
  496.  
  497.     SendMessage(hLbxRDir,LB_ADDSTRING,0,(LPARAM)"..");
  498.     if(nRC==FTP_COMPLETE) {
  499.       if((fd=fopen(szTmpFile,"r"))!=NULL) {
  500.         while(fgets(szString,180,fd)!=NULL) {
  501.           if((pStr=strchr(szString,'\n'))!=NULL) *pStr=0;
  502.           switch(nHostType+6000) {
  503.             case DLG_HOST_SUPER:
  504.             case DLG_HOST_CHAMELEON:
  505.             case DLG_HOST_NCSA:
  506.               if(strstr(szString,"<DIR>")!=NULL) {
  507.                 if((pStr=strchr(szString,' '))!=NULL) *pStr=0;
  508.                 if(strcmp(szString,".")!=0 && strcmp(szString,"..")!=0)
  509.                   SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)szString);
  510.               } else {
  511.                 if((pStr=strchr(szString,' '))!=NULL) *pStr=0;
  512.                 if(szString[0]!=0)
  513.                   SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)szString);
  514.               }
  515.               break;
  516.             case DLG_HOST_QVT:
  517.               if(szString[strlen(szString)-1]=='/' || 
  518.                  szString[strlen(szString)-1]=='\\')
  519.               {
  520.                 szString[strlen(szString)-1]=0;
  521.                 if(strcmp(szString,".")!=0 && strcmp(szString,"..")!=0)
  522.                   SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)szString);
  523.               } else
  524.                 SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)szString);
  525.               break;
  526.             case DLG_HOST_IBM_VM:
  527.               szString[12]=0;
  528.               SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)szString);
  529.               break;
  530.             case DLG_HOST_VMS_UCX:
  531.             case DLG_HOST_VMS_MULTINET:
  532.               if(*szString!=' ') {
  533.                 if((s=strchr(szString,';'))!=NULL) {
  534.                   if(strlen(szString)>4 &&
  535.                      strcmp(&szString[strlen(szString)-4],".DIR")==0) {
  536.                     *s=0;
  537.                     szString[strlen(szString)-4]=0;
  538.                     SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)szString);
  539.                   } else {
  540.                     if((s=strchr(szString,' '))!=NULL) *s=0;
  541.                     if((s=strchr(szString,'\t'))!=NULL) *s=0;
  542.                     SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)szString);
  543.                   }
  544.                 }
  545.               }
  546.               break;
  547.             case DLG_HOST_PCTCP:
  548.               szString[30]=0; s=FindName(szString);
  549.               if(strncmp(szString,"<dir>",5)==0) {
  550.                 if(strcmp(s,".")!=0 && strcmp(s,"..")!=0)
  551.                   SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)s);
  552.               } else
  553.                 SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)s);
  554.               break;
  555.             case DLG_HOST_IBM_TCP:
  556.               s=FindName(szString);
  557.               if(strstr(szString," DIR ")!=NULL) {
  558.                 if(strcmp(s,".")!=0 && strcmp(s,"..")!=0)
  559.                   SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)s);
  560.               } else
  561.                 SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)s);
  562.               break;            
  563.             case DLG_HOST_NOS:
  564.               if(strstr(szString,"Disk size")==NULL) {
  565.                 szString[13]=0; nRC=13;
  566.                 while((nRC=strlen(szString))>0 && szString[nRC-1]==' ')
  567.                   szString[nRC-1]=0;
  568.                 if(*szString!=0) {
  569.                   if(szString[strlen(szString)-1]=='/' || 
  570.                      szString[strlen(szString)-1]=='\\')
  571.                   {
  572.                     szString[strlen(szString)-1]=0;
  573.                     if(strcmp(szString,".")!=0)
  574.                       SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)szString);
  575.                   } else
  576.                     SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)szString);
  577.                 }
  578.                 pStr=&szString[41]; nRC=13; pStr[nRC]=0;
  579.                 while((nRC=strlen(pStr))>0 && pStr[nRC-1]==' ')
  580.                   pStr[nRC-1]=0;
  581.                 if(*pStr!=0) {
  582.                   if(pStr[strlen(pStr)-1]=='/' ||
  583.                      pStr[strlen(pStr)-1]=='\\')
  584.                   {
  585.                     pStr[strlen(pStr)-1]=0;
  586.                     if(strcmp(pStr,".")!=0 && strcmp(pStr,"..")!=0)
  587.                       SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)pStr);
  588.                   } else
  589.                     SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)pStr);
  590.                 }
  591.               }
  592.               break;
  593.             case DLG_HOST_SINTFTPD:
  594.             case DLG_HOST_U5000:
  595.             case DLG_HOST_UNIX:
  596.             default:
  597.               // assume UNIX ls format
  598.               pStr=FindName(szString);
  599.               // if line starts with 'd' its a directory
  600.               if(strchr("dl",szString[0])!=NULL) {
  601.                 if(strcmp(pStr,".")!=0 && strcmp(pStr,"..")!=0)
  602.                   SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)pStr);
  603.               } else
  604.               // if line starts with - or f its a file
  605.               if(nHostType==(DLG_HOST_SINTFTPD-6000) ||
  606.                  strchr("-f",szString[0])!=NULL) {
  607.                 SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)pStr);
  608.               }
  609.               break;
  610.           }
  611.         }
  612.         fclose(fd);
  613.       } else
  614.         DoAddLine("couldn't open tmpfile for read.");
  615.     } else
  616.       DoPrintf("DoDirList returned %u",nRC);
  617.   } // if we were connected
  618.   return 0;
  619. }
  620.  
  621. /*
  622.   Read the output from HELP and see if we can determine the hosttype
  623.   (if we don't already know it) and determine what functions are
  624.   valid (things like mkdir, etc)
  625. */
  626. int ReadProcessHelp(SOCKET ctrl_socket)
  627. {
  628.   int iRetCode;
  629.  
  630.   bCanMKD=bCanRMD=bCanREN=bCanDELE=FALSE;
  631.  
  632.   if(SendPacket(ctrl_socket,"HELP")!=-1) {
  633.     iRetCode=ReadLine(ctrl_socket);
  634.     if((iRetCode/100)==5 && nHostType==0) nHostType=DLG_HOST_NOS-6000;
  635.     else {
  636.       if(nHostType==0) {
  637.         if(strstr(szMsgBuf,"NCSA")!=NULL ||
  638.            strstr(szMsgBuf,"CUTCP")!=NULL)
  639.           nHostType=DLG_HOST_NCSA-6000;
  640.         else if(strncmp(szMsgBuf,"214-PC FTP server",17)==0 ||
  641.               strstr(szMsgBuf,"QVT")!=NULL)
  642.           nHostType=DLG_HOST_QVT-6000;
  643.         else
  644.           nHostType=DLG_HOST_UNIX-6000;
  645.       }
  646.       while((iRetCode!=421) && ((iRetCode/100)!=2 || szMsgBuf[3]=='-')) {
  647.         if(strstr(szMsgBuf,"MKD")!=NULL) bCanMKD=TRUE;
  648.         if(strstr(szMsgBuf,"RMD")!=NULL) bCanRMD=TRUE;
  649.         if(strstr(szMsgBuf,"RNFR")!=NULL) bCanREN=TRUE;
  650.         if(strstr(szMsgBuf,"DELE")!=NULL) bCanDELE=TRUE;
  651.         iRetCode=ReadLine(ctrl_socket);
  652.       }
  653.     }
  654.   }
  655.   EnableWindow(hBtnRMKD,bCanMKD);
  656.   EnableWindow(hBtnRRMD,bCanRMD);
  657.   EnableWindow(hBtnRREN,bCanREN);
  658.   EnableWindow(hBtnRDEL,bCanDELE);
  659.   bHELP=TRUE;
  660.   return iRetCode;
  661. }
  662.  
  663. //*************************************************//
  664. // routines to load and save our list of hostnames //
  665. //*************************************************//
  666. int GPPS(LPSTR fldname,LPSTR deflt,LPSTR destination,int len) {
  667.   return(GetPrivateProfileString(szAppName,fldname,
  668.               deflt,destination,len,szIniFile));
  669. }
  670.  
  671. void WPPS(LPSTR fldname,LPSTR value) {
  672.   WritePrivateProfileString(szAppName,fldname,value,szIniFile);
  673. }
  674.             
  675. void LoadUserInfo()
  676. {
  677.   UINT flags;
  678.   int nIndex;
  679.   LPSTR s;
  680.  
  681.   GPPS("MAILADDR",NULL,szMailAddress,127);
  682.   GPPS("VIEWER","notepad", szViewer,120);
  683.   bAutoStart=GetPrivateProfileInt(szAppName, "AUTOSTART",
  684.            bAutoStart,szIniFile);
  685.   flags=GetPrivateProfileInt(szAppName,"FLAGS",
  686.            64+4+1,szIniFile);
  687.   if(flags & 1) bRecvUniq=1; else bRecvUniq=0;
  688.   if(flags & 2) bStorUniq=1; else bStorUniq=2;
  689.   if(flags & 4) bBell=1; else bBell=0;
  690.   if(flags & 8) bInteractive=1; else bInteractive=0;
  691.   if(flags & 16) bVerbose=1; else bVerbose=0;
  692.   if(flags & 32) bHash=1; else bHash=2;
  693. //if(flags & 64) bSendPort=1; else bSendPort=0;
  694.   if(flags & 128) bDoGlob=1; else bDoGlob=2;
  695.   for(nIndex=0;nIndex<MAXHOSTS;nIndex++) {
  696.     wsprintf(szString,"HST%u",nIndex);
  697.     if(GPPS(szString,NULL,szRemoteHost,79)>0) {
  698.       if((s=strchr(szRemoteHost,' '))!=NULL) {
  699.         *s++=0;
  700.         nHostType=atoi(s);
  701.         if((s=strchr(s,' '))!=NULL) {
  702.           *s++=0;
  703.           uiTimeOut=atoi(s);
  704.         } else uiTimeOut=65;
  705.       } else { nHostType=0; uiTimeOut=65;}
  706.       szUserID[0]=0; 
  707.       wsprintf(szString,"UID%u",nIndex);
  708.       GPPS(szString,NULL,szUserID,79);
  709.       szPassWord[0]=0;
  710.       wsprintf(szString,"PWD%u",nIndex);
  711.       GPPS(szString,NULL,szPassWord,79);
  712.       szInitDir[0]=0;
  713.       wsprintf(szString,"DIR%u",nIndex);
  714.       GPPS(szString,NULL,szInitDir,79);
  715.       HostInfo[nIndex]=MakeInfo(szRemoteHost,szUserID,
  716.         szPassWord,szInitDir,nHostType,1024,1024,uiTimeOut);
  717.     } else HostInfo[nIndex]=NULL;
  718.   }
  719.   GPPS("HOSTNAME","129.29.64.246",szRemoteHost,79);
  720.   GPPS("USERID","anonymous", szUserID, 80);
  721.   szPassWord[0]=0; szInitDir[0]=0; uiTimeOut=65; nHostType=0;
  722. }
  723.  
  724. void SaveUserInfo()
  725. {
  726.   UINT flags;
  727.   int nIndex;
  728.  
  729.   WPPS(NULL,NULL);
  730.   WPPS("HOSTNAME",szRemoteHost);
  731.   WPPS("USERID",szUserID);
  732.   WPPS("MAILADDR",szMailAddress);
  733.   WPPS("VIEWER",szViewer);
  734.   wsprintf(szString,"%u",bAutoStart);  WPPS("AUTOSTART",szString);
  735.   flags=((bRecvUniq==1)?1:0) +
  736. //              ((bStorUniq==1)?2:0) +
  737.                 ((bBell==1)?4:0) +
  738.                 ((bInteractive==1)?8:0)+
  739. //              ((bHash==1)?32:0) +
  740. //              ((bSendPort==1)?64:0) +
  741. //              ((bDoGlob==1)?128:0) +
  742.                 ((bVerbose==1)?16:0);
  743.   wsprintf(szString,"%u",flags);       WPPS("FLAGS",szString);
  744.   for(nIndex=0;nIndex<MAXHOSTS;nIndex++)
  745.     if(GetInfo(HostInfo[nIndex],szRemoteHost,szUserID,szPassWord,szInitDir,
  746.       &nHostType,NULL,NULL,&uiTimeOut)) {
  747.       wsprintf(szString,"HST%u",nIndex);
  748.       if(szRemoteHost[0]!=0) {
  749.         wsprintf(szMsgBuf,"%s %u %u",szRemoteHost,nHostType,uiTimeOut);
  750.         WPPS(szString,szMsgBuf);
  751.         wsprintf(szString,"UID%u",nIndex);
  752.         if(szUserID[0]!=0)        WPPS(szString,szUserID);
  753.         wsprintf(szString,"PWD%u",nIndex);
  754.         if(szPassWord[0]!=0)      WPPS(szString,szPassWord);
  755.         wsprintf(szString,"DIR%u",nIndex);
  756.         if(szInitDir[0]!=0)       WPPS(szString,szInitDir);
  757.         FreeInfo(HostInfo[nIndex]);
  758.         HostInfo[nIndex]=0;
  759.       }
  760.     }
  761. }
  762.  
  763.